/*
 * NumberObject.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;
import org.scriptme.ecmascript.exceptions.ProgrammingError;
import org.scriptme.ecmascript.interpreter.Evaluator;

/**
 * Implemements the EcmaScript Number singleton.
 */
public class NumberObject extends BuiltinFunctionObject {

	/**
	 * Instantiates a new number object.
	 * 
	 * @param prototype
	 *            the prototype
	 * @param evaluator
	 *            the evaluator
	 */
	private NumberObject(ESObject prototype, Evaluator evaluator) {
		super(prototype, evaluator, "Number", 1);
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.BuiltinFunctionObject#toString()
	 */
	public String toString() {
		return "<Number>";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.FunctionPrototype#callFunction(org.scriptme.ecmascript.data.ESObject,
	 *      org.scriptme.ecmascript.data.ESValue[])
	 */
	public ESValue callFunction(ESObject thisObject, ESValue[] arguments)
			throws EcmaScriptException {
		if (arguments.length == 0) {
			return new ESNumber(0.0);
		} else {
			return new ESNumber(arguments[0].doubleValue());
		}
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#doConstruct(org.scriptme.ecmascript.data.ESObject,
	 *      org.scriptme.ecmascript.data.ESValue[])
	 */
	public ESObject doConstruct(ESObject thisObject, ESValue[] arguments)
			throws EcmaScriptException {
		NumberPrototype theObject = null;
		ESObject np = evaluator.getNumberPrototype();
		theObject = new NumberPrototype(np, evaluator);
		if (arguments.length > 0) {
			theObject.value = new ESNumber(arguments[0].doubleValue());
		} else {
			theObject.value = new ESNumber(0.0);
		}
		return theObject;
	}

	/**
	 * Utility function to create the single Number object.
	 * 
	 * @param evaluator
	 *            the Evaluator
	 * @param objectPrototype
	 *            The Object prototype attached to the evaluator
	 * @param functionPrototype
	 *            The Function prototype attached to the evaluator
	 * 
	 * @return the Number singleton
	 */
	public static NumberObject makeNumberObject(Evaluator evaluator,
			ObjectPrototype objectPrototype, FunctionPrototype functionPrototype) {

		NumberPrototype numberPrototype = new NumberPrototype(objectPrototype,
				evaluator);
		NumberObject numberObject = new NumberObject(functionPrototype,
				evaluator);

		try {

			// For numberPrototype
			class NumberPrototypeToString extends BuiltinFunctionObject {
				NumberPrototypeToString(String name, Evaluator evaluator,
						FunctionPrototype fp) {
					super(fp, evaluator, name, 1);
				}

				public ESValue callFunction(ESObject thisObject,
						ESValue[] arguments) throws EcmaScriptException {
					ESValue v = ((NumberPrototype) thisObject).value;
					String s = v.toString();
					if (arguments.length > 0) {
						double d = arguments[0].doubleValue();
						if (!Double.isNaN(d)) {
							s = Long
									.toString(((long) v.doubleValue()), (int) d);
						}
					}
					return new ESString(s);
				}
			}
			class NumberPrototypeValueOf extends BuiltinFunctionObject {
				NumberPrototypeValueOf(String name, Evaluator evaluator,
						FunctionPrototype fp) {
					super(fp, evaluator, name, 1);
				}

				public ESValue callFunction(ESObject thisObject,
						ESValue[] arguments) throws EcmaScriptException {
					return ((NumberPrototype) thisObject).value;
				}
			}

			numberObject.putHiddenProperty("prototype", numberPrototype);
			numberObject.putHiddenProperty("length", new ESNumber(1));
			numberObject.putHiddenProperty("MAX_VALUE", new ESNumber(
					Double.MAX_VALUE));
			numberObject.putHiddenProperty("MIN_VALUE", new ESNumber(
					Double.MIN_VALUE));
			numberObject.putHiddenProperty("NaN", new ESNumber(Double.NaN));
			numberObject.putHiddenProperty("NEGATIVE_INFINITY", new ESNumber(
					Double.NEGATIVE_INFINITY));
			numberObject.putHiddenProperty("POSITIVE_INFINITY", new ESNumber(
					Double.POSITIVE_INFINITY));

			numberPrototype.putHiddenProperty("constructor", numberObject);
			numberPrototype.putHiddenProperty("toString",
					new NumberPrototypeToString("toString", evaluator,
							functionPrototype));
			numberPrototype.putHiddenProperty("valueOf",
					new NumberPrototypeValueOf("valueOf", evaluator,
							functionPrototype));
		} catch (EcmaScriptException e) {
			e.printStackTrace();
			throw new ProgrammingError(e.getMessage());
		}

		evaluator.setNumberPrototype(numberPrototype);

		return numberObject;
	}
}
